#pragma once

#include <iostream>
#include <string>
#include <ctemplate/template.h>
#include <boost/asio.hpp>
#include <boost/beast.hpp>
#include <boost/beast/core.hpp>

#include "oj_model_mq.hpp"
#include "oj_control.hpp"

namespace ns_view
{
    using namespace ns_model;
    using namespace boost::asio;
    using namespace boost::asio::ip;
    using namespace boost::beast;

    const std::string template_path = "./template_html/";
    class View
    {
    public:
        View() {}
        ~View() {}
        void AllExpandHtml(const vector<struct Question> &questions, std::string *html)
        {
            // 1、形成渲染目标路径
            std::string src_html = template_path + "all_questions.html";

            // 2、形成数据字典
            ctemplate::TemplateDictionary root("all_questions");
            for (const auto &q : questions)
            {
                ctemplate::TemplateDictionary *sub = root.AddSectionDictionary("questions_list"); // 添加sub子字典
                sub->SetValue("number", q.number);
                sub->SetValue("title", q.title);
                sub->SetValue("star", q.star);
            }

            // 3、获取被渲染的网页
            ctemplate::Template *tpl = ctemplate::Template::GetTemplate(src_html, ctemplate::DO_NOT_STRIP);

            // 4、开始进行渲染
            tpl->Expand(html, &root);
        }

        void OneExpandHtml(const struct Question &q, std::string *html)
        {
            // 1、形成渲染目标路径
            std::string src_html = template_path + "one_question.html";
            // 2、获取数据字典
            ctemplate::TemplateDictionary root("one_question");
            root.SetValue("number", q.number);
            root.SetValue("title", q.title);
            root.SetValue("star", q.star);
            root.SetValue("desc", q.desc);
            root.SetValue("pre_code", q.header);

            // 3、获取被渲染的html
            ctemplate::Template *tpl = ctemplate::Template::GetTemplate(src_html, ctemplate::DO_NOT_STRIP);

            // 4、开始渲染
            tpl->Expand(html, &root);
        }
    };
}
